<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class showinvoice_emailpdf {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('paperpdf');
    $masterclass->loadmodule('companies');
    $masterclass->loadmodule('captcha',$masterclass->mainconfig['v4rootdir'].'lib/captcha.lib.php');

    $masterclass->loadmodule('paperpdf_render',$masterclass->mainconfig['v4moduledir'].'paperpdf/paperpdf_render.func.php');
    
    $masterclass->loadjavascript('',$masterclass->mainconfig['v4rootdir'].'include/suckertreemenu.inc.js');

    $masterclass->site['postfields'][]=array('trackingid'=>'invoicesid');
    
  return;
  }
  function savedata() {
    global $masterclass;
    
    foreach ($masterclass->sitevars['post_action'] as $postaction) {
      switch ($postaction) {
       case 'invoices_sendpdfemail':
         if ($masterclass->site['captcha_post_auth']=='') { break; }

        $strwhere=sprintf(" (`id`=%d) ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($masterclass->sitevars['invoicesid']));

        $invoicesinfo=$masterclass->fetch_records('`invoices_documents`',1,$strwhere,'','*');
        $invoicesitems=$masterclass->allclasses['invoices']->fetch_invoices_items($masterclass->sitevars['invoicesid'],'',' `datestamp` ');

        $masterclass->sitevars['pdfdata']=array();
        $masterclass->sitevars['pdfdata']['pdfname']='invoice_'.$invoicesinfo->fields['id'].'.pdf';
        $masterclass->sitevars['pdfdata']['pdfforms'][]=array('pdflibrary'=>'pdf_invoice','pdflibrary_path'=>$masterclass->mainconfig['v4moduledir'].'invoices/pdf_forms/pdf_invoice.lib.php','pdfdata'=>array('invoiceinfo'=>$invoicesinfo,'invoiceitems'=>$invoicesitems));

        $pdffile=$masterclass->site['server_path'].$masterclass->websiteconfig['invoices_emaildir'].'pdfinvoiceid-'.$invoicesinfo->fields['id'].'.pdf';
        $pdfdoc=$masterclass->allclasses['paperpdf_render']->render_document('S');
        file_put_contents($pdffile,$pdfdoc);

        if ($invoicesinfo->fields['net']=='0') {
          $duenet='Upon Receipt';
        } else { $duenet='Net '.$invoicesinfo->fields['net']; }
        $txtbody='';
        $txtbody.=$masterclass->sitevars['invoices_email_text']."\n\n";
        $txtbody.='Invoice #'.$invoicesinfo->fields['documentid'];$txtbody.="\n";
        $txtbody.='Date: '.$invoicesinfo->fields['documentdate'];$txtbody.="\n";
        $txtbody.='Due: '.$invoicesinfo->fields['duedate'].' ['.$duenet.']';$txtbody.="\n\n";

        $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($invoicesitems);
        $txtbody.='Items: '.$invoicetotals['qty'];$txtbody.="\n";
        $txtbody.='Subtotal: $'.number_format($invoicetotals['subtotal'],2);$txtbody.="\n";
        $txtbody.='Tax: $'.number_format($invoicetotals['tax'],2);$txtbody.="\n";
        $txtbody.='Total: $'.number_format($invoicetotals['total'],2);$txtbody.="\n\n";
        $txtbody.=$masterclass->allclasses['invoices']->display_invoices_itemstxt($invoicesitems)."\n\n";

        $masterclass->loadmodule('PHPMailer',$masterclass->mainconfig['v4rootdir'].'lib/class.phpmailer.php');

        $masterclass->allclasses['PHPMailer']->IsQMail();

        $strwhere=sprintf(" `id`=%d ",$masterclass->format_dbl($masterclass->site['companyid']));
        $companyinfo=$masterclass->fetch_records('`companies`',1,$strwhere);
        $masterclass->allclasses['PHPMailer']->From = $companyinfo->fields['mail'];
        $masterclass->allclasses['PHPMailer']->FromName = $companyinfo->fields['name'];
        $masterclass->allclasses['PHPMailer']->AddReplyTo($companyinfo->fields['email'],$companyinfo->fields['name']);

        $masterclass->allclasses['PHPMailer']->AddAddress($masterclass->sitevars['recipient_to_email'],$masterclass->sitevars['recipient_to_name']);

        $masterclass->allclasses['PHPMailer']->Subject = stripslashes($masterclass->sitevars['invoices_email_subject']);
        $masterclass->allclasses['PHPMailer']->Body=stripslashes($txtbody);

        $masterclass->allclasses['PHPMailer']->AddAttachment($pdffile,'invoice_'.$invoicesinfo->fields['documentid'],'base64','application/pdf');
      
        if(!$masterclass->allclasses['PHPMailer']->Send()) { ?><script language="javascript">alert('Message Failed, Try Again');</script><?php $masterclass->site['forward']=''; } else { $masterclass->site['forward']='window.close();'; }

       break;
      }
    }

  return;
  }
  function display() {
    global $masterclass;

    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }

    $strwhere=sprintf(" (`id`=%d) ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($masterclass->sitevars['invoicesid']));
    $invoicesinfo=$masterclass->fetch_records('`invoices_documents`',1,$strwhere,'','*');
    $invoicesitems=$masterclass->allclasses['invoices']->fetch_invoices_items($masterclass->sitevars['invoicesid'],'',' `datestamp` ');

    $masterclass->sitevars['recipient_to_name']=$invoicesinfo->fields['recipientlineone'];
    if (isset($invoicesinfo->fields['customer']) && $invoicesinfo->fields['customer']!='' && $invoicesinfo->fields['customer']!=0) {
      $strwhere=sprintf(" (`id`=%d) ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($invoicesinfo->fields['customer']));
      $customerinfo=$masterclass->fetch_records('`customers`',1,$strwhere,'','*');
      if (!empty($customerinfo)) {
        if ($customerinfo->fields['emaila']!='') {
          $masterclass->sitevars['recipient_to_email']=$customerinfo->fields['emaila'];
        } else {
          $masterclass->sitevars['recipient_to_email']=$customerinfo->fields['emailb'];
        }
        if ($masterclass->sitevars['recipient_to_name']=='') { $masterclass->sitevars['recipient_to_name']=$customerinfo->fields['first'].' '.$customerinfo->fields['last']; }
      }
    }
    $masterclass->sitevars['invoices_email_subject']='Invoice #'.$invoicesinfo->fields['documentid'];

    $pageheader='<table width="100%" cellpadding="0" cellspacing="0" border="0" class="sitebox_pageheader"><tr>';
    $pageheader.='<td style="text-align:left;">E-Mail Invoice '.$invoicesinfo->fields['recipientlineone'].'</td>';
    $pageheader.='<td style="text-align:right;" width="25"><nobr>#'.$invoicesinfo->fields['documentid'].'</nobr></td>';
    $pageheader.='</tr></table>';
    echo $masterclass->allclasses['websites']->sitebox_pageheader($pageheader,'');

$nav='';
$nav.='<div class="sitebox_navigation" style="width:100%;">';
$nav.='<ul id="sitebox_navmenu">';
$nav.='<li OnClick="captcha_action(\'add_post_action(\\\'invoices_sendpdfemail\\\');document.getElementById(\\\'frm\\\').submit();\');"><a>Send E-Mail</a></li>';
$nav.='<li OnClick="window.close();"><a>Close</a></li>';
$nav.='</ul>';
$nav.='<br style="clear: left;" />';
$nav.='</div>';
echo $nav;
?>
<table width="100%" cellpadding="2" cellspacing="5" border="0">
  <tr valign="top">
  <td width="100%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('E-Mail Recipient','');
?>
<table width="100%" align="center" cellpadding="2" cellspacing="0" class="sitebox_content">
   <tr>
     <td>Name</td>
     <td width="95%" valign="middle"><?php echo $masterclass->allclasses['websites']->disp_inputtext('recipient_to_name');?></td>
    </tr>
   <tr>
     <td>E-Mail</td>
     <td width="95%" valign="middle"><?php echo $masterclass->allclasses['websites']->disp_inputtext('recipient_to_email');?></td>
    </tr>
  </table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
echo $masterclass->allclasses['websites']->sitebox_vspacer();
echo '<div class="sitebox_content">Attachments: <b>invoice_'.$invoicesinfo->fields['documentid'].'.pdf</b>&nbsp&nbsp&nbsp;[<a href="showinvoice_pdf.php?nohtml=1&invoicesid='.$invoicesinfo->fields['id'].'" target="_blank">View</a>]</div>';
echo $masterclass->allclasses['websites']->sitebox_vspacer();
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('E-Mail Message','');
?>
<table width="100%" align="center" cellpadding="2" cellspacing="0" class="sitebox_content">
   <tr>
     <td>Subject</td>
     <td width="95%" valign="middle"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_email_subject');?></td>
    </tr>
   <tr valign="absmiddle">
     <td colspan="2" width="95%" valign="middle"><?php echo $masterclass->allclasses['websites']->disp_textarea('invoices_email_text','95%','8','40');?></td>
    </tr>
  </table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
  </td>
  </tr>
</table>
<?php
/*
    $masterclass->allclasses['sitevars']['pdfdata']=array();
    $masterclass->allclasses['sitevars']['pdfdata']['pdfname']='doc.pdf';
    $masterclass->allclasses['sitevars']['pdfdata']['pdfforms'][]=array('pdflibrary'=>'pdf_invoice','pdflibrary_path'=>$masterclass->mainconfig['v4moduledir'].'invoices/pdf_forms/pdf_invoice.lib.php','pdfdata'=>array('invoiceinfo'=>$invoicesinfo,'invoiceitems'=>$invoicesitems));

    $masterclass->allclasses['paperpdf_render']->render_document();
*/
  return;
  }
}
?>
